package finance

import grails.test.*
import org.joda.time.*
import org.joda.time.contrib.hibernate.*

class HistoricalQueryServiceTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

	def historicalQueryService
	
    void testQueryVals() {
		def type = new InstrumentType(type:"type", description:"desc")
		assertNotNull "Type should not be null", type.save()
		
		def ds = new Datasource(name:"DASOURCE", description: "desc")
		assertNotNull "DS should not be null", ds.save()
		
		def ins = new Instrument(name: "TEST", symbol: "TEST", 
			source: ds, instrumentType : type)
		
		assertNotNull "Instrument should not be null",ins.save()
		
		def f = new Field(name:"TOAST", description: "desc")
		assertNotNull "f should not be null", f.save()
		
		def v = new FieldValue(instrument:ins, field:f,
			added: new DateTime(), val: "1.0")
		assertNotNull "V should not be null", v.save()
		
		def vals = historicalQueryService.queryVals("TEST", "TOAST")
		assertNotNull "Vals should not be null",vals
		assertEquals "There should be 1 value", vals.size() , 1
    }
	
	void testQueryPairVals() {
		def type = new InstrumentType(type:"type", description:"desc")
		assertNotNull "Type should not be null", type.save()
		
		def ds = new Datasource(name:"DASOURCE", description: "desc")
		assertNotNull "DS should not be null", ds.save()
		
		def ins = new Instrument(name: "TEST", symbol: "TEST", 
			source: ds, instrumentType : type)
		def ins2 = new Instrument(name: "TEST2", symbol: "TEST2", 
			source: ds, instrumentType : type)
		
		assertNotNull "Instrument should not be null",ins.save()
		assertNotNull "Instrument should not be null",ins2.save()
		
		def f = new Field(name:"TOAST", description: "desc")
		assertNotNull "f should not be null", f.save()
	
		def now = new DateTime()	
		def v = new FieldValue(instrument:ins, field:f,
			added: now, val: "1.0")
		def v2 = new FieldValue(instrument:ins2, field:f,
			added: now, val: "3.0")
		assertNotNull "V should not be null", v.save()
		assertNotNull "V2 should not be null", v2.save()
		
		def vals = historicalQueryService.queryPairVals("TEST", "TEST2", "TOAST")
		assertNotNull "Vals should not be null",vals
		assertEquals "There should be 1 value", vals.size() , 1
	}
}
